import 'dart:async';
import 'dart:io';

import 'package:love_newf/app/com/base/app.dart';
import 'package:love_newf/app/com/base/L.dart';
import 'package:love_newf/app/com/base/base_state.dart';
import 'package:love_newf/app/com/manage/txim_manage/im_manage.dart';
import 'package:love_newf/app/com/plugin/face_recognition_plugin.dart';
import 'package:love_newf/app/com/util/permission_utils.dart';
import 'package:love_newf/app/page/login/login_page.dart';
import 'package:love_newf/generated/assets.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:liuxlcommon/util/sp_utils.dart';
import 'package:love_newf/app/com/view/common_view.dart';
import 'package:love_newf/app/page/home/home_page.dart';
import 'login_logic.dart';

class LaunchPage extends StatefulWidget {
  const LaunchPage({super.key});

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LaunchPage>
    with SingleTickerProviderStateMixin {
  bool isfrist = false;
  bool isAgree = false;
  bool initLanguge = false;

  @override
  void didUpdateWidget(covariant LaunchPage oldWidget) {
    super.didUpdateWidget(oldWidget);
    // precacheImage(const AssetImage(Assets.pcLargebg), context);
  }

  @override
  void initState() {
    super.initState();
    lazyLogic();
    Get.delete<HomePage>();
    var widgetsBinding = WidgetsBinding.instance;
    widgetsBinding.addPostFrameCallback((callback) async {
      var agree = SpUtils().getStorage1("agree", false);
      if (!agree) {
        CommonView.loginAgreenDialog(onConfirm: () {
          SpUtils().setStorage("agree", true);
          init();
        });
      }
      if (agree) init();
    });
  }

  init() async {
    if (!isfrist) {
      isfrist = true;
      if (Platform.isIOS) {
        if (!await checkInternetConnection(isNetwork: true)) {}
      } else {}
      initLanguage();
      if (Platform.isAndroid) {
        // await platformAndroid();
        isAgree = true;
        next();
      } else {
        await platformIos();
      }
    }
  }

  // platformAndroid() async {
  //   isAgree = SpUtils().getStorage1("loginAgreenDialog", false);
  //   initLanguge = true;
  //   if (isAgree) {
  //     next();
  //   } else {
  //     CommonView.loginAgreenDialog(onConfirm: () async {
  //       SpUtils().setStorage("loginAgreenDialog", true);
  //       Get.back();
  //       await ReportedDataService().checkPermission();
  //       isAgree = true;
  //       next();
  //     });
  //   }
  // }

  platformIos() async {
    // await CommonPlugin.getWaitIdea();
    await PermissionUtils.checkPermission(goToSetting: false);
    isAgree = true;
    next();
  }

  next() async {
    if (isAgree && initLanguge) {
      await App.init2();
      if (L.loginUserInfo?.saToken == null) {
        Get.offAll(LoginPage());
      } else {
        if (await ImManage().login()) {
          Get.offAll(HomePage());
        }
      }

      // if (App.localeEn) {
      //   Get.offAll(const c.HomePage());
      // } else {
      // if (SpUtils().getStorage1("isGuide", true)) {
      //   SpUtils().setStorage("isGuide", false);
      //   Get.offAll(const GuidePage());
      // } else {
      //   Get.offAll(HomePage());
      // }
      // }
      return;
    }
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // precacheImage(const AssetImage(Assets.bgABg1), context);
    if (SpUtils().getStorage1("isGuide", true)) {}
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Image.asset(Assets.imgLaunchBg,
            width: double.infinity,
            height: double.infinity,
            fit: BoxFit.cover));
  }

  int count = 0;

  initLanguage() async {
    var data = null;
    data = await L.appConfig;

    try {
      data = await L.appConfig;
    } catch (e) {
      print(e);
    }
    if (data != null) {
      initLanguge = true;
      next();
    } else {
      count++;
      if (count > 3) {
        // exit(1);
        return;
      }
      await Future.delayed(const Duration(seconds: 1));
      await initLanguage();
    }
  }

  @override
  void dispose() {
    Get.delete<LoginLogic>();
    super.dispose();
  }
}
